#include <libbase/Worker.h>

namespace BASE{
TaskThread::TaskThread(ITask * _task, const std::string & _name):RefObj(), mTask(_task), name(_name){

}

void TaskThread::setId(int mId){
	mTask->setId(mId);
}

TaskThread::~TaskThread(){

}

void TaskThread::thread(){
	while(isRunning()){
		mTask->run();
	}
}

Worker::Worker(const std::string & name): RefObj(), mName(name){

}

Worker::~Worker(){
	workers.clear();	
}

bool Worker::create(ITask * _task, int num){
	if (_task == NULL){
		return false;
	}
	TaskThread *tt = new TaskThread(_task, mName);
	tt->setId(0);
	tt->start();
	workers.push_back(tt);
	for (int i = 1; i < num; ++i){
		ITask * task = _task->clone();
		TaskThread *thr = new TaskThread(task, mName);
		thr->setId(i);
		thr->start();
		workers.push_back(thr);
	}
	return true;
}

void Worker::shutdown(){
	workers.clear();
}

}
